package com.dnjn.happiness.emo.handler;

import com.dnjn.happiness.common.core.constant.SecurityConstants;
import com.dnjn.happiness.common.core.domain.R;
import com.dnjn.happiness.common.core.model.SysJobVO;
import com.dnjn.happiness.common.core.utils.StringUtils;
import com.dnjn.happiness.emo.domain.UserRecordTask;
import com.dnjn.happiness.emo.service.IUserRecordTaskService;
import com.dnjn.happiness.job.api.RemoteJobService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component("taskHandler")
public class TaskHandler {

    @Autowired
    private RemoteJobService jobService;

    @Autowired
    private IUserRecordTaskService userRecordTaskService;

    public void theTimedOut(String taskId, String jobId) {

        UserRecordTask one = userRecordTaskService.lambdaQuery().eq(UserRecordTask::getTaskId, taskId).eq(UserRecordTask::getDeleted, '0').one();
        if (null != one) {
            String taskStatus = one.getTaskStatus();
            if (null != taskStatus && !taskStatus.equals("F_DONE")) {
                userRecordTaskService.lambdaUpdate().set(UserRecordTask::getTaskStatus, "T_TIMED_OUT").eq(UserRecordTask::getDeleted, '0').eq(UserRecordTask::getTaskId, taskId).update();
            }
        }
        //仅仅运行一次
        SysJobVO newJob = new SysJobVO();
        R<SysJobVO> result = jobService.getInfoById(Long.parseLong(jobId), SecurityConstants.INNER);
        if (StringUtils.isNotNull(result) && StringUtils.isNotNull(result.getData())) {
            newJob = result.getData();
        }
        newJob.setStatus("1");
        jobService.changeStatusApi(newJob, SecurityConstants.INNER);

    }

}
